package com.szy.assembly;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;

public class Ram implements Serializable {
    private int ramSize;
    public HashMap<Integer,Integer> ram;

    public ArrayList<Peripheral> peripherals;

    public Ram(int ramSize, ArrayList<Peripheral> peripherals){
        this.ramSize=ramSize;
        this.peripherals=peripherals;
        ram=new HashMap<>();
    }

    public void set(int address,int value){
        if(address < ramSize) {
            ram.put(address, value);
        }else{
            for(Peripheral peripheral:peripherals){
                peripheral.setRegister(address,value);
            }
        }
    }

    public int get(int address){
        if(address < ramSize){
            return ram.getOrDefault(address,0);
        }else {
            for(Peripheral peripheral:peripherals){
                int[] res=peripheral.getRegister(address);
                if(res[0] != 0){
                    return res[1];
                }
            }
            return 0;
        }
    }
}
